package com.modules.service.sys.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.common.util.AES2;
import com.common.vo.Result;
import com.modules.dao.sys.MenuDao;
import com.modules.dao.sys.OrganizationDao;
import com.modules.entity.sys.OrganizationEntity;
import com.modules.service.sys.OrganizationService;

import cn.hutool.core.util.StrUtil;


/**
 * service组织
 *
 * @author Administrator
 */
@Service
public class OrganizationServiceImpl implements OrganizationService{

    private final Logger logger = Logger.getLogger(OrganizationServiceImpl.class);

    @Autowired
    private OrganizationDao dao;
    @Autowired
    private MenuDao mdao;

    /**
     * 查询单个
     * @return
     */
    public Result<?> selectOne(OrganizationEntity org) {
        OrganizationEntity organization = dao.selectOne(new LambdaQueryWrapper<OrganizationEntity>().eq(OrganizationEntity::getOid, org.getOid()));
        return Result.success(organization, "获取成功");
    }

    /**
     * 查询
     * @return
     */
    public Result<?> select(Integer oid) {
    	List<Map<String, Object>> lv = new ArrayList<Map<String, Object>>();
        List<OrganizationEntity> list = dao.selectList(new LambdaQueryWrapper<OrganizationEntity>().eq(OrganizationEntity::getOid, oid));
        for (int i = 0; i < list.size(); i++) {
            OrganizationEntity p = list.get(i);
            Map<String, Object> map = new HashMap<>();
            map.put("oid", p.getOid());
            map.put("abbreviation", p.getOname());
            map.put("oname", p.getOname());
            map.put("fid", p.getParentId());
            map.put("isproject", p.getIsProject());
            map.put("spread", true);
            map.put("children", selectChil(p.getOid()));
            lv.add(map);
        }
        return Result.success(lv, "获取成功");
    }

    public List<Map<String, Object>> selectChil(Integer fid) {
        List<Map<String, Object>> lv = new ArrayList<Map<String, Object>>();
        List<OrganizationEntity> list = dao.selectList(new LambdaQueryWrapper<OrganizationEntity>().eq(OrganizationEntity::getParentId, fid));
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            OrganizationEntity p = list.get(i);
            map.put("oid", p.getOid());
            map.put("abbreviation", p.getOname());
            map.put("oname", p.getOname());
            map.put("fid", p.getParentId());
            map.put("isproject", p.getIsProject());
            map.put("spread", true);
            map.put("children", selectChil(p.getOid()));
            lv.add(map);
        }
        return lv;
    }

    /**
     * 查询所有项目
     * @return
     */
    public Result<?> selectAllProject(Integer oid) {
    	List<OrganizationEntity> list = dao.selectAllProject(oid, 1);
        return Result.success(list, "获取成功");
    }

    /**
     * 保存
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> save(OrganizationEntity org, String menu0, String menu1, String menu2) {
    	if (StrUtil.isEmpty(org.getOname())) {
            return Result.fail("参数错误");
        }
        LambdaQueryWrapper<OrganizationEntity> queryWrapper = new LambdaQueryWrapper<OrganizationEntity>();
        queryWrapper.eq(OrganizationEntity::getOname, org.getOname());
        if (org.getOid() != null) {
        	queryWrapper.ne(OrganizationEntity::getOid, org.getOid());
        }
        Integer count = dao.selectCount(queryWrapper);
        if (count > 0) {
            return Result.fail("组织/项目名称重复");
        }
        if (org.getOid() == null) {
            dao.insert(org);
        } else {
            dao.updateById(org);
        }
        return Result.success("保存成功");
    }

    /**
     * 删除
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> delete(Integer oid) {
    	Integer count = dao.selectCount(new LambdaQueryWrapper<OrganizationEntity>().eq(OrganizationEntity::getParentId, oid));
        if (count > 0) {
            return Result.fail("删除失败，请先删除下级");
        }
        dao.deleteById(oid);
        return Result.success("删除成功");
    }
    
    /**
     * 生成账号秘钥
     * @param value
     * @return
     */
	public Result<?> createCodeAndPass(String value){
		try {
			if(StrUtil.isEmpty(value)) {
				return Result.fail("参数错误");
			}
			String code = AES2.Encrypt(value, "szsgstar_ddmcode");// 得到账号
			String pass = AES2.Encrypt(value, "szsgstar_ddmpass");// 得到密钥
			return Result.success(new String[]{code,pass}, "获取成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            return Result.fail("系统异常："+e.getMessage());
        }
	}



}
